#!/bin/bash
# 测试使用Turritopsis方法修复的EbbFlow

set -euo pipefail

echo "=== 测试Turritopsis方法修复的EbbFlow ==="

# 清理旧进程
echo "清理旧进程..."
pkill -f "ebbflow" || true
pkill -f "waterbear" || true
sleep 2

# 清理端口
echo "清理端口..."
for port in 8000 8001 8002 8003 8004 8005; do
    fuser -k -n tcp $port 2>/dev/null || true
done
sleep 1

# 构建EbbFlow
echo "构建EbbFlow..."
cd /home/ebbflow
go build -o bin/ebbflow_server src/main/ebbflow_server.go
go build -o bin/ebbflow_client src/main/ebbflow_client.go

echo "✅ EbbFlow构建成功"

# 启动EbbFlow服务器
echo "启动EbbFlow服务器..."
for i in 0 1 2 3; do
    echo "启动节点 $i"
    nohup ./bin/ebbflow_server -id $i > logs/node-$i.log 2>&1 &
    sleep 1
done

# 等待服务器启动
echo "等待服务器启动..."
sleep 5

# 启动客户端
echo "启动客户端..."
nohup ./bin/ebbflow_client -id 0 -config etc/conf.json -batchsize 500 -txsize 250 > logs/client.log 2>&1 &

# 运行测试
echo "运行测试30秒..."
sleep 30

# 停止所有进程
echo "停止所有进程..."
pkill -f "ebbflow" || true
sleep 2

# 分析结果
echo "=== 分析结果 ==="
echo "检查崩溃情况..."
if grep -q "panic" logs/node-*.log; then
    echo "❌ 发现崩溃:"
    grep -n "panic" logs/node-*.log
else
    echo "✅ 无崩溃"
fi

echo "检查epoch执行情况..."
if grep -q "epoch.*completed" logs/node-*.log; then
    echo "✅ 发现epoch执行:"
    grep "epoch.*completed" logs/node-*.log | tail -5
else
    echo "❌ 未发现epoch执行"
fi

echo "检查并发控制情况..."
if grep -q "concurrency control" logs/node-*.log; then
    echo "✅ 发现并发控制:"
    grep "concurrency control" logs/node-*.log | tail -3
else
    echo "❌ 未发现并发控制"
fi

echo "检查TPS情况..."
if grep -q "TPS" logs/client.log; then
    echo "✅ 发现TPS:"
    grep "TPS" logs/client.log | tail -3
else
    echo "❌ 未发现TPS"
fi

echo "=== 测试完成 ==="


